local locks = require "resty.lock"
local cjson = require "cjson"
local headers = ngx.req.get_headers()
local messageUri = ngx.var.uri
local resp_body = ngx.arg[1]
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
    ngx.var.resp_body = ngx.ctx.buffered
end

if 2 ==2  then                      -- --这里是开关，要不要缓存数据
    local cache_list = ngx.shared.cache_list
    --local keys = cache_list:get_keys(1000)  -- 一秒处理 50 个数据
    --
    --for index, key in pairs(keys) do
    --    str = ((nil ~= str) and str..[[#]]..cache_list:get(key)) or cache_list:get(key)
    --    ngx.log(ngx.ERR,"key="..key.." str=" ..str)
    --   -- ngx_share:delete(key)  --干掉这个 key
    --end
    local current_url= ngx.var.uri
    local  cache_url= cache_list:get(current_url)  or ""

    if not (cache_url == "") then
        local  cache_data= cache_list:get(messageUri..ngx.var.mumuYuyinKey..'data')  or ""
        local cache_json = cjson.decode(cache_url)
        if  (cache_data == "") then

            local mumuYuyinKey=ngx.var.mumuYuyinKey or ''
            cache_list:set(messageUri..mumuYuyinKey..'data', ngx.var.resp_body, cache_json["cache_time"])
            ngx.log(ngx.ERR, messageUri..mumuYuyinKey.."  cache_time="..cache_json["cache_time"])

        end

    end


end


--local    regularStr = string.find(ngx.var.resp_body, '"code":200')   --打印返回结果非200的异常信息
--if not regularStr and ngx.var.resp_body ~=''  and messageUri ~='/api/agora/callback/sha1/v1'  then
--    local mumuYuyinKey=ngx.var.mumuYuyinKey or ''
--
--    ngx.log(ngx.ERR," codeNot200  auth:",headers["auth"] ,"  url= ", messageUri   , "   mumulog : ","  request_body_lua:",mumuYuyinKey,"  reponse_body: ",ngx.var.resp_body," not regularStr ",not regularStr)
--end
